﻿Imports System.Drawing
Imports System.Drawing.Drawing2D

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class ImageButton
    Inherits System.Windows.Forms.Button

#Region "Designer Generated Code"
    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private WithEvents components As System.Windows.Forms.Button

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'components
        '
        Me.components.Location = New System.Drawing.Point(0, 0)
        Me.components.Name = "ImageButton"
        Me.components.Size = New System.Drawing.Size(75, 23)
        Me.components.TabIndex = 0
        '
        'VerticalContainerLabel
        '
        Me.Name = "ImageButton"
        Me.ResumeLayout(False)
    End Sub
#End Region

    Private iMouseOver As Boolean = False
    Private iMouseDown As Boolean = False
    Private iBorders As Boolean = True
    Public Property Borders As Boolean
        Get
            Return Me.iBorders
        End Get
        Set(value As Boolean)
            Me.iBorders = value
            Me.Invalidate()
        End Set
    End Property
    Private iNormalBorderDark As Color = Color.FromArgb(112, 112, 112)
    Public Property NormalBorderDark As Color
        Get
            Return Me.iNormalBorderDark
        End Get
        Set(value As Color)
            Me.iNormalBorderDark = value
        End Set
    End Property
    Private iNormalBorderLight1 As Color = Color.FromArgb(207, 207, 207)
    Public Property NormalBorderLight1 As Color
        Get
            Return Me.iNormalBorderLight1
        End Get
        Set(value As Color)
            Me.iNormalBorderLight1 = value
        End Set
    End Property
    Private iNormalBorderLight2 As Color = Color.FromArgb(242, 242, 242)
    Public Property NormalBorderLight2 As Color
        Get
            Return Me.iNormalBorderLight2
        End Get
        Set(value As Color)
            Me.iNormalBorderLight2 = value
        End Set
    End Property
    Private iActiveBorderDark As Color = Color.FromArgb(60, 127, 177)
    Public Property ActiveBorderDark As Color
        Get
            Return Me.iActiveBorderDark
        End Get
        Set(value As Color)
            Me.iActiveBorderDark = value
        End Set
    End Property
    Private iActiveBorderLight1 As Color = Color.FromArgb(167, 217, 245)
    Public Property ActiveBorderLight1 As Color
        Get
            Return Me.iActiveBorderLight1
        End Get
        Set(value As Color)
            Me.iActiveBorderLight1 = value
        End Set
    End Property
    Private iActiveBorderLight2 As Color = Color.FromArgb(234, 246, 253)
    Public Property ActiveBorderLight2 As Color
        Get
            Return Me.iActiveBorderLight2
        End Get
        Set(value As Color)
            Me.iActiveBorderLight2 = value
        End Set
    End Property
    Private iHoverImage As Image = Nothing
    Public Property HoverImage As Image
        Get
            Return Me.iHoverImage
        End Get
        Set(value As Image)
            Me.iHoverImage = value
        End Set
    End Property

    Protected Overrides Sub OnEnabledChanged(e As System.EventArgs)
        MyBase.OnEnabledChanged(e)
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        'MyBase.OnPaint(e)

        'Clear background
        Dim backBrush As New SolidBrush(Me.BackColor)
        e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), Me.ClientRectangle)

        'Avoid 0 division calculations
        If Me.ClientRectangle.Width < 8 Or Me.ClientRectangle.Height < 8 Then Exit Sub

        Dim gh As New GraphicsHelper

        Dim cRect As New Rectangle(Me.ClientRectangle.Left, _
                                    Me.ClientRectangle.Top, _
                                    Me.ClientRectangle.Width - 1, _
                                    Me.ClientRectangle.Height - 1)

        If Me.iBorders Then
            'Draw dark border
            Dim borderRect As New Rectangle(Me.ClientRectangle.Left, _
                                            Me.ClientRectangle.Top, _
                                            Me.ClientRectangle.Width - 1, _
                                            Me.ClientRectangle.Height - 1)
            Dim iBorderDarkPen As Pen = Nothing
            If iMouseDown Then
                iBorderDarkPen = New Pen(Me.iActiveBorderDark, 2)
            Else
                If iMouseOver Then
                    iBorderDarkPen = New Pen(Me.iActiveBorderDark)
                Else
                    iBorderDarkPen = New Pen(Me.iNormalBorderDark)
                End If
            End If

            e.Graphics.DrawPath(iBorderDarkPen, gh.GetRoundCornerPathForRectangle(borderRect))

            'Draw light border
            Dim borderRect2 As New Rectangle(borderRect.Left + 2, _
                                                        borderRect.Top + 2, _
                                                        borderRect.Width - 4, _
                                                        borderRect.Height - 4)
            Dim iBorderPenLight As Pen = Nothing
            If Me.iMouseDown Then
                iBorderPenLight = New Pen(New LinearGradientBrush(borderRect2, _
                                                                  Me.iActiveBorderLight2, _
                                                                  Me.iActiveBorderLight1, _
                                                                  LinearGradientMode.ForwardDiagonal), 2)
            Else
                If iMouseOver Then
                    iBorderPenLight = New Pen(New LinearGradientBrush(borderRect2, _
                                                                      Me.iActiveBorderLight2, _
                                                                      Me.iActiveBorderLight1, _
                                                                      LinearGradientMode.ForwardDiagonal))
                Else
                    iBorderPenLight = New Pen(New LinearGradientBrush(borderRect2, _
                                                                      Me.iNormalBorderLight2, _
                                                                      Me.iNormalBorderLight1, _
                                                                      LinearGradientMode.ForwardDiagonal))
                End If
            End If
            e.Graphics.DrawPath(iBorderPenLight, gh.GetRoundCornerPathForRectangle(borderRect2))

            'Get the actual contents rectangle
            cRect = New Rectangle(borderRect2.Left + 2, _
                                    borderRect2.Top + 2, _
                                    borderRect2.Width - 4, _
                                    borderRect2.Height - 4)

        End If

        If Me.Image IsNot Nothing Then
            If Me.Text <> "" Then
                If (Me.Width > Me.Height) AndAlso (Me.Height < 60) Then
                    'Draw text on right
                    Dim tRect As New Rectangle(cRect.X + cRect.Height, cRect.Y, cRect.Width - cRect.Height - 1, cRect.Height)
                    'Draw the text
                    Dim sf As New StringFormat
                    sf.Alignment = StringAlignment.Center
                    sf.LineAlignment = StringAlignment.Center
                    gh.DrawTextNormal(e.Graphics, Me.Text, Me.Font, Me.ForeColor, tRect, sf)

                    'Draw the image on the left
                    Dim iRect As New Rectangle(cRect.X, cRect.Y, cRect.Height, cRect.Height)
                    If (Me.iHoverImage IsNot Nothing) AndAlso (Me.iMouseOver) Then
                        e.Graphics.DrawImage(Me.iHoverImage, iRect)
                    Else
                        e.Graphics.DrawImage(Me.Image, gh.GetImageRectangleProportionalCentered(Me.Image, iRect))
                    End If
                Else
                    Dim tRect As Rectangle = gh.GetTextRectangle(Me.Text, Me.Font, cRect, e.Graphics, AlighnmentSidesEnum.Bottom)
                    'Draw the text
                    Dim sf As New StringFormat
                    sf.Alignment = StringAlignment.Center
                    sf.LineAlignment = StringAlignment.Center
                    gh.DrawTextNormal(e.Graphics, Me.Text, Me.Font, Me.ForeColor, tRect, sf)

                    'Get the image rectangle
                    Dim iRect As New Rectangle(cRect.X, cRect.Y, cRect.Width, cRect.Height - tRect.Height - 1)
                    If (Me.iHoverImage IsNot Nothing) AndAlso (Me.iMouseOver) Then
                        e.Graphics.DrawImage(Me.iHoverImage, iRect)
                    Else
                        e.Graphics.DrawImage(Me.Image, gh.GetImageRectangleProportionalCentered(Me.Image, iRect))
                    End If
                End If
            Else
                If (Me.iHoverImage IsNot Nothing) AndAlso (Me.iMouseOver) Then
                    e.Graphics.DrawImage(Me.iHoverImage, cRect)
                Else
                    e.Graphics.DrawImage(Me.Image, cRect)
                End If
            End If
        Else
            If Me.Text <> "" Then
                'Draw the text
                Dim sf As New StringFormat
                sf.Alignment = StringAlignment.Center
                sf.LineAlignment = StringAlignment.Center
                gh.DrawTextNormal(e.Graphics, Me.Text, Me.Font, Me.ForeColor, cRect, sf)
            End If
        End If

        If Me.Enabled = False Then
            Dim trGray As Color = Color.FromArgb(127, Me.BackColor)
            e.Graphics.FillRectangle(New SolidBrush(trGray), cRect)
        End If

    End Sub

    Protected Overrides Sub OnMouseDown(mevent As System.Windows.Forms.MouseEventArgs)
        Me.iMouseDown = True
        MyBase.OnMouseDown(mevent)
    End Sub

    Protected Overrides Sub OnMouseUp(mevent As System.Windows.Forms.MouseEventArgs)
        Me.iMouseDown = False
        MyBase.OnMouseUp(mevent)
    End Sub

    Protected Overrides Sub OnGotFocus(e As System.EventArgs)
        Me.iMouseOver = True
        MyBase.OnGotFocus(e)
    End Sub

    Protected Overrides Sub OnLostFocus(e As System.EventArgs)
        Me.iMouseOver = False
        MyBase.OnLostFocus(e)
    End Sub

    Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
        Me.iMouseOver = True
        MyBase.OnMouseEnter(e)
    End Sub

    Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
        Me.iMouseOver = False
        MyBase.OnMouseLeave(e)
    End Sub

    Enum AlighnmentSidesEnum
        Left
        Right
        Top
        Bottom
    End Enum

End Class

